/**
 * Copyright (C), 2024, xxxx有限公司
 * FileName: AddAdderssBookConfig
 * Author:   TLVM
 * Date:     4/25/2024 1:00 AM
 * Description: 添加联系人/通讯录配置
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.iqcctt.orangelightaiassistant.call;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.iqcctt.common.PhoneValidUtil;
import com.iqcctt.orangelightaiassistant.entity.ChatBook;
import com.iqcctt.orangelightaiassistant.service.ChatBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.function.Function;

/**
 * 〈一句话功能简述〉<br>
 * 〈添加联系人/通讯录配置〉
 *
 * @author TLVM
 * @create 4/25/2024
 * @since 1.0.0
 */
@Slf4j
public class AdderssBookService implements Function<AdderssBookService.Request, AdderssBookService.Response> {
    /**
     * Weather Function request.
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonClassDescription("添加通讯录/联系人API接口信息")
    public record Request(@JsonProperty(required = true,
            value = "userName") @JsonPropertyDescription("姓名字段,例如:张三,李四,王五,tom") String userName,
                          @JsonProperty(required = false,
            value = "phone") @JsonPropertyDescription("手机号码字段,例如:17789899898,13425256969") String phone,
                          @JsonProperty(required = false,
            value = "email") @JsonPropertyDescription("邮箱地址字段,例如:iqcctt@gmail.com,iqcctt@outlook.com等等") String email) {
    }

    /**
     * Weather Function response.
     */
    public record Response(String weather) {
    }

    private final ChatBookService chatBookService;

    public AdderssBookService(ChatBookService chatBookService) {
        this.chatBookService = chatBookService;
    }

    public AdderssBookService.Response apply(AdderssBookService.Request request) {
        log.info("request参数-------姓名 :{},电话:{},邮箱地址:{}", request.userName,request.phone,request.email);
        log.debug("调用添加联系人API接口，根据AI返回参数进行回调、AI回答参数：{}",request);
        try{
            if(request.phone!=null){
                Assert.isTrue(PhoneValidUtil.isValidPhoneNumber(request.phone), "用户的发送的手机号码格式不正确。");
            }
            if(request.email!=null){
                Assert.isTrue(Validator.isEmail(request.email), "用户发送的邮箱格式不正确。");
            }
            chatBookService.save(ChatBook.builder()
                    .name(request.userName)
                    .phone(request.phone)
                    .email(request.email)
                    .createTime(DateUtil.currentSeconds())
                    .build());
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof IllegalArgumentException){
                return new AdderssBookService.Response(e.getMessage());
            }
            return new AdderssBookService.Response("执行异常,程序异常.");
        }
        return new AdderssBookService.Response("保存成功!");
    }
}
